package com.poobo.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.poobo.base.impl.BaseDaoImpl;
import com.poobo.condiction.ResourcesConfigCondiction;
import com.poobo.dao.IResourcesConfigDao;
import com.poobo.entity.TbConfigList;
import com.poobo.entity.TbResourcesConfig;
import com.poobo.util.date.DateUtil;
import com.poobo.util.pager.Page;

@Repository
public class ResourcesConfigDaoImpl extends BaseDaoImpl implements IResourcesConfigDao{

	@Override
	public TbResourcesConfig queryResourcesConfigById(Integer id) {
		return selectObjectById(TbResourcesConfig.class, id);
	}

	@Override
	public Page queryResourcesConfigPage2(TbResourcesConfig resourcesConfig,
			Page page) {
		String hql = " from TbResourcesConfig where 1=1 ";
		String countHql = " select count(*) from TbResourcesConfig where 1=1 ";
		List<Object> params = new ArrayList<Object>();
		String orderBy = " order by id desc";
		StringBuffer where = new StringBuffer();
		if( resourcesConfig != null ){
			if( resourcesConfig.getConfigId() != null ){
				where.append(" and configId = ? ");
				params.add(resourcesConfig.getConfigId());
			}
		}
		page.setBaseHql(hql + where.toString() + orderBy);
		page.setCountHql(countHql + where.toString());
		page.setParam(params);
		return getPageByHql(page);
	}

	@Override
	public void deleteResourcesConfigById(Integer id) {
		String hql = " delete from TbResourcesConfig where id = "+ id +" ";
		delObjectByIds(hql);
//		getSession().flush();
	}

	@Override
	public void saveResourcesConfig(TbResourcesConfig resourcesConfig) {
		saveOrUpdate(resourcesConfig);
	}

	@Override
	public Page queryResourcesConfigPage( ResourcesConfigCondiction condiction , Page page) {
		String baseSql = "select reco.rcid as id, reco.rid as resourcesId,reco.type as type," +
				"reco.rname as resourcesName,reco.cid as configId,reco.cname as configName," +
				"reco.DisplayOrder as displayOrder, reco.DateCompany as dateCompany, reco.isSpecial, " +
				"reco.dayOne,reco.dayTwo,reco.dayThree,reco.dayFour,reco.dayFive,reco.isLive, " +
				"reco.ActionDate as actionDate,reco.endDate as endDate,reco.week,reco.seconds " +
				"from(select rc.ID as rcid, r.ID as rid,r.type as type, c.ID as cid, r.`NAME` as rname," +
				"c.`name` as cname,rc.DisplayOrder,rc.DateCompany,rc.ActionDate,rc.endDate,rc.week,rc.seconds, " +
				"rc.isSpecial,rc.dayOne,rc.dayTwo,rc.dayThree,rc.dayFour,rc.dayFive ,rc.isLive " +
				"from tb_resources_config rc " +
				"left join tb_resources r on rc.resourcesId=r.ID " +
				"left join tb_config_list c on rc.configId=c.ID where 1=1 ";
		String countSql = " select count(*) "+
				"from(select rc.ID as rcid, r.ID as rid, c.ID as cid, r.`NAME` as rname," +
				"c.`name` as cname,rc.DisplayOrder ,rc.DateCompany,rc.ActionDate,rc.endDate " +
				"from tb_resources_config rc " +
				"left join tb_resources r on rc.resourcesId=r.ID " +
				"left join tb_config_list c on rc.configId=c.ID where 1=1 ";
		StringBuffer where = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		String groupBy = " ) as reco group by reco.rcid ";
		String orderBy = "  order by reco.cid DESC,reco.DisplayOrder ASC ";
		
		if ( condiction.getConfigName() != null && condiction.getConfigName() != "") {
			where.append(" and c.`name` like '%" + condiction.getConfigName() + "%' ");
		}
		if ( condiction.getWeek() != null ) {
			where.append(" and rc.week="+condiction.getWeek()+" ");
		}
		if ( condiction.getId() != null ) {
			where.append(" and rc.ID =" + condiction.getConfigName() + " ");
		}
		page.setBaseSql(baseSql + where.toString() + groupBy + orderBy);
		page.setCountSql(countSql + where.toString() + groupBy);
		page.setParam(params);
		Page page2 = getPageBySql(page);
		page2.setPageList(getMapBySql(baseSql + where.toString() + groupBy + orderBy, null));
		return page2;
	}

	@Override
	public List<TbResourcesConfig> queryResourcesConfigByConfigListId(
			Integer configListId) {
		String hql = " from TbResourcesConfig where configId="+configListId+" order by displayOrder asc";
		return (List<TbResourcesConfig>)findAll(hql);
	}

	@Override
	public void deleteResourcesConfigByResourcesId(Integer resourcesId) {
		String sql = " delete from tb_resources_config where resourcesId = "+ resourcesId +" ";
		execSql(sql);
	}

	@Override
	public void deleteResourcesConfigByConfigListId(Integer configId) {
		String sql = " delete from tb_resources_config where configId = "+ configId +" ";
		execSql(sql);
//		getSession().flush();
	}

	@Override
	public String copyResourcesConfigByConfigListId(Integer configId) {
		TbConfigList configList = selectObjectById(TbConfigList.class, configId);
		TbConfigList cfl = new TbConfigList();
		if (configList!=null) {
			cfl.setIsEnable(configList.getIsEnable());
			cfl.setIsGenerationTxt(configList.getIsGenerationTxt());
			cfl.setName(configList.getName()+"_"+DateUtil.getNowTimeStr("yyyyMMddhhmmss"));
			saveOrUpdate(cfl);
			return configList.getName()+"_"+DateUtil.getNowTimeStr("yyyyMMddhhmmss");
		}else {			
			return null;
		}
	}
}
